// pages/user/userCenterNew/comps/userData/userData.js
const md5 = require('../../../../../utils/md5.js')
const common = require('../../../../../utils/util.js')
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        userInfo: Object,
        messageInfo: Object,
        unionID: Number
    },

    /**
     * 组件的初始数据
     */
    data: {
        roleList: ['游客', '普通会员', 'V1', 'V2', 'V3']
    },

    /**
     * 组件的方法列表
     */
    methods: {
        getUserInfo: function(e) {
            const that = this
            if (e.detail.errMsg == 'getUserInfo:ok') {
                wx.showLoading({
                    title: '登录中'
                })
                wx.checkSession({
                    success() {
                        //session_key 未过期，并且在本生命周期一直有效
                        let data = {
                            code: wx.getStorageSync('code'),
                            phone: wx.getStorageSync('userPhone'),
                            wx_sm: 1,
                            encryptedData: e.detail.encryptedData,
                            iv: e.detail.encryptedData
                        }
                        common.post('Login/phone_unionid', data).then(function(res) {
                            wx.hideLoading()
                            wx.login({
                                success(loginRes) {
                                    wx.setStorageSync('code', loginRes.code)
                                }
                            })
                            if (res.code == 204) {
                                wx.showModal({
                                    title: '提示',
                                    content: '网络繁忙，请重新登录',
                                    showCancel: false,
                                    success(res) {}
                                })
                                return
                            } else {
                                wx.setStorageSync('unionID', 1)
                                common.refresh()
                            }
                        }).catch(function(e) {})
                    },
                    fail() {
                        // session_key 已经失效，需要重新执行登录流程
                        wx.login({
                            success(res) {
                                if (res.code) {
                                    let data = {
                                        code: res.code,
                                        phone: wx.getStorageSync('userPhone'),
                                        wx_sm: 1,
                                        encryptedData: e.detail.encryptedData,
                                        iv: e.detail.iv
                                    }
                                    common.post('Login/phone_unionid', data).then(function(res) {
                                        wx.hideLoading()
                                        wx.login({
                                            success(loginRes) {
                                                wx.setStorageSync('code', loginRes.code)
                                            }
                                        })
                                        if (res.code == 204) {
                                            wx.showModal({
                                                title: '提示',
                                                content: '网络繁忙，请重新登录',
                                                showCancel: false,
                                                success(res) {}
                                            })
                                            return
                                        } else {
                                            wx.setStorageSync('unionID', 1)
                                            common.refresh()
                                        }
                                    }).catch(function(e) {})
                                }
                            }
                        })
                    }
                })
            }
        },

        goUserInfo: function() {
            wx.navigateTo({
                url: '/pages/user/userData/userData'
            })
        },

        goConcatInviter: function() {
            if (this.data.userInfo.parent_phone_c == 0) {
                wx.showToast({
                    title: '暂无邀请人',
                    icon: 'none',
                    duration: 2000
                })
                return
            }
            wx.makePhoneCall({
                phoneNumber: this.data.userInfo.parent_phone_c
            })
        },

        goIncome: function(e) {
            wx.navigateTo({
                url: '/pages/user/accumulatedIncome/accumulatedIncome?type=' + e.currentTarget.dataset.type
            })
        },

        goAccount: function() {
            wx.navigateTo({
                url: '/pages/user/account/account'
            })
        },

        goContribution: function() {
            wx.navigateTo({
                url: '/pages/user/contribution/contribution'
            })
        },

        goShopkeeperGifts: function() {
            wx.navigateTo({
                url: '/pages/goods/shopKeeperGifts/shopKeeperGifts'
            })
        },

        goTeamData: function() {
            wx.navigateTo({
                url: '/pages/user/team/teamData/teamData'
            })
        },

        goTeamList: function(e) {
            wx.navigateTo({
                url: '/pages/user/team/teamList/teamList?type=' + e.currentTarget.dataset.type + '&typename=' + e.currentTarget.dataset.typename
            })
        },

        goMessageCenter: function() {
            wx.navigateTo({
                url: '/pages/messageSystem/messageCenter/messageCenter',
            })
        }
    }
})